package com.ququ.syb.entrance.service;

import com.ququ.syb.common.constant.EncryptTypeConstant;
import com.ququ.syb.common.pojo.PayChannel;
import com.ququ.syb.entrance.fegin.ChannelBusinessFegin;
import com.ququ.syb.entrance.po.RequestData;
import com.ququ.syb.entrance.vo.SybResult;
import com.ququ.syb.entrance.vo.SybResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PayChannelService {
    @Autowired
    private SecurityService securityService;
    @Autowired
    private ChannelBusinessFegin channelBusinessFegin;

    public SybResult listMyPayChannels(RequestData<String> requestData){
        if (requestData.getEncryptType() != EncryptTypeConstant.AES)
            return new SybResult(-1,"加密方式不正确",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String,Object> dataMap = securityService.getParametersMap(requestData);
        List<PayChannel> payChannelList = channelBusinessFegin.listMyPayChannels(requestData.getAccountid(),requestData.getAppcode()).getData();
        if (payChannelList == null)
            return new SybResult(-1,"没有可用的支付通道",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        List<Map> resultList= new ArrayList<>();
        for (PayChannel payChannel : payChannelList){
            Map<String,Object> channel = new HashMap<>();
            channel.put("paychannelid",payChannel.getPaychannelid());
            channel.put("paychannelname",payChannel.getPaychannelname());
            channel.put("customercommissionrate",payChannel.getCustomercommissionrate());
            channel.put("companyname",payChannel.getCompanyname());
            channel.put("maxpayamount",payChannel.getMaxpayamount());
            channel.put("minpayamount",payChannel.getMinpayamount());
            channel.put("withdrawtype",payChannel.getWithdrawtype());
            channel.put("needcvv",payChannel.getNeedcvv());
            channel.put("webpay",payChannel.getWebpay());
            channel.put("needconfirm",payChannel.getNeedconfirm());
            channel.put("channeltype",payChannel.getChanneltype());
            resultList.add(channel);
        }
        return securityService.buildSybResult(resultList,requestData);
    }


    public SybResult getPayChannel(RequestData<String> requestData){
        if (requestData.getEncryptType() != EncryptTypeConstant.AES)
            return new SybResult(-1,"加密方式不正确",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String,Object> dataMap = securityService.getParametersMap(requestData);
        if (!dataMap.containsKey("paychannelid"))
            return new SybResult(-1,"通道号参数丢失",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        PayChannel payChannel = channelBusinessFegin.getPayChannel(dataMap.get("paychannelid").toString(),requestData.getAccountid(),requestData.getAppcode()).getData();
        if (payChannel == null)
            return new SybResult(-1,"无此支付通道或支付通道不可用",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String,Object> channel = new HashMap<>();
        channel.put("paychannelid",payChannel.getPaychannelid());
        channel.put("paychannelname",payChannel.getPaychannelname());
        channel.put("customercommissionrate",payChannel.getCustomercommissionrate());
        channel.put("companyname",payChannel.getCompanyname());
        channel.put("maxpayamount",payChannel.getMaxpayamount());
        channel.put("minpayamount",payChannel.getMinpayamount());
        channel.put("withdrawtype",payChannel.getWithdrawtype());
        channel.put("needcvv",payChannel.getNeedcvv());
        channel.put("webpay",payChannel.getWebpay());
        channel.put("needconfirm",payChannel.getNeedconfirm());
        channel.put("channeltype",payChannel.getChanneltype());
        return securityService.buildSybResult(channel,requestData);
    }


}
